import json

from django.http import JsonResponse

# Create your views here.
from django.views.decorators.csrf import csrf_exempt

from baobaoxiong.check_phone import check_phone
from baobaoxiong.upload_down import upload_img, download
from schoolmaster.models import School, Users, Role
from teachers.models import Teacher


@csrf_exempt
def index_views(request):
    uphone = request.POST.get('headermasterphone')
    school = School.objects.filter(headmasterphone=uphone, isDlete=False).first()
    upload_img(str(school.headmasterphoto))  # 上传图片到七牛云
    url = download(str(school.headmasterphoto))
    if school:
        # 查询园所对应的老师
        teachers = school.teachers.all().filter(isDelete=False)
        if teachers:
            lst_teacher = []
            for tea in teachers:
                upload_img(str(tea.teacherphoto))  # 上传图片到七牛云
                stu_url = download(str(tea.teacherphoto))
                dic = {
                    'tid': tea.id,  # 老师id
                    'photo': stu_url,  # 老师头像
                    'tname': tea.uname,  # 姓名
                    'uphone': tea.uphone  # 电话
                }
                lst_teacher.append(dic)

            dic1 = {
                'uname': school.headmastername,  # 园长姓名
                'img': url,  # 园长头像
                'version': school.version,  # 版本信息
                'starttime': school.starttime,  # 开始时间
                'endtime': school.endtime,  # 到期时间
                'teachers': lst_teacher,  # 老师信息
            }
            return JsonResponse(dic1)
        else:
            dic1 = {
                'uname': school.headmastername,  # 园长姓名
                'img': url,  # 园长头像
                'version': school.version,  # 版本信息
                'starttime': school.starttime,  # 开始时间
                'endtime': school.endtime,  # 到期时间
                'teachers': [],  # 老师信息
            }
            return JsonResponse(dic1)


@csrf_exempt
def sure_add_views(request):
    uname = request.POST.get('uname')
    uphone = request.POST.get('uphone')
    if Teacher.objects.filter(uphone=uphone):
        return JsonResponse({'status': 'error', 'text': '该电话已存在'})
    elif check_phone(uphone) == 'fail':
        return JsonResponse({'status': 'failed', 'text': '电话错误'})
    masterphone = request.POST.get('headermasterphone')
    school = School.objects.filter(headmasterphone=masterphone).first()
    # 保存到老师表
    teacher = Teacher()
    teacher.uname = uname
    teacher.uphone = uphone
    teacher.school_id = school.id
    teacher.save()

    # 保存到用户表
    user = Users.objects.filter(uphone=uphone).first()
    if user:
        role = Role.objects.get(id=3)
        user.role_id = role.id
        user.save()
        teacher1 = Teacher.objects.filter(uphone=uphone).first()
        teacher1.user_id = user.id
        teacher1.save()

    else:
        user1 = Users()
        user1.uphone = uphone
        role = Role.objects.get(id=3)
        user1.role_id = role.id
        user1.save()
        teacher2 = Teacher.objects.filter(uphone=uphone).first()
        teacher2.user_id = Users.objects.filter(uphone=uphone).first().id
        teacher2.save()

    return JsonResponse({'status': 'ok'})


@csrf_exempt
def modify_tea_views(request):
    tid = request.POST.get('tid')
    uphone = request.POST.get('uphone')
    if Teacher.objects.filter(uphone=uphone):
        return JsonResponse({'status': 'error', 'text': '该电话已存在'})
    elif check_phone(uphone) == 'fail':
        return JsonResponse({'status': 'failed', 'text': '电话错误'})
    teacher = Teacher.objects.get(id=tid)
    teacher.uphone = uphone
    teacher.save()

    # 到用户表修改电话
    teacher = Teacher.objects.filter(uphone=uphone).first()
    user = teacher.user
    user.uphone = uphone
    user.save()
    return JsonResponse({'status': 'ok'})


@csrf_exempt
def delete_tea_views(request):
    uphone = request.POST.get('uphone')
    teacher = Teacher.objects.filter(uphone=uphone).first()
    teacher.isDelete = True
    teacher.save()

    # 查询用户表，改角色
    teacher = Teacher.objects.filter(uphone=uphone).first()
    user = teacher.user
    role = Role.objects.get(id=2)
    user.role_id = role.id
    user.save()
    return JsonResponse({'status': 'ok'})


